//获取应用实例
const app = getApp();

Page({
  data: {
    pic: '',
    title: '',
    type: '',
    isColours: '',
    id: '',
    ccinfo: '',
    pagesType: 'tp',
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '预览', //导航栏 中间的标题
    },
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.height * 2 + 20,
  },
  onLoad(options) {
    console.log(options)
    let that = this;
    that.setData({
      pic: options.pic,
      pagesType: options.pagesType,
      pics: options.pic,
      title: options.title,
      type: options.type,
      isColours: options.isColours,
      id: options.id,
      ccinfo: options.ccinfo
    })

  },
  godingdan: function () {
    let pics = this.data.pic;
    if (this.data.pagesType == 'FY') {
      wx.navigateBack();
    } else {
      wx.navigateTo({
        url: '/modelA/pages/drawings_printing/picplace_order/picplace_order?pics=' + pics + '&title=' + this.data.title + '&type=' + this.data.type + '&isColours=' + this.data.isColours + '&id=' + this.data.id + '&ccinfo=' + this.data.ccinfo,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  //点击保存图片
  save() {
    let that = this
    //若二维码未加载完毕，加个动画提高用户体验
    wx.showToast({
      icon: 'loading',
      title: '正在保存图片',
      duration: 1000
    })
    //判断用户是否授权"保存到相册"
    wx.getSetting({
      success(res) {
        //没有权限，发起授权
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {//用户允许授权，保存图片到相册
              that.savePhoto();
            },
            fail() {//用户点击拒绝授权，跳转到设置页，引导用户授权
              wx.openSetting({
                success() {
                  wx.authorize({
                    scope: 'scope.writePhotosAlbum',
                    success() {
                      that.savePhoto();
                    }
                  })
                }
              })
            }
          })
        } else {//用户已授权，保存到相册
          that.savePhoto()
        }
      }
    })
  },
  //保存图片到相册，提示保存成功
  savePhoto() {
    let that = this
    wx.downloadFile({
      url: that.data.pic,
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: "success",
              duration: 1000
            })
          }
        })
      }
    })
  }

})
